
/*
 
The application sends increasing counter value to Zabbix in the form of traps.
To use in a meaningful manner, fire up your Zabbix UI or API, and create a
hostname "arduino", add an item with key "counter" of type "Zabbix trapper",
with type "Numeric".
 
*/
 
#include <Arduino.h>
#include <SPI.h>
#include <Ethernet.h>


#include "dht.h"


#define dht_apin A0 // Analog Pin sensor is connected to
 
dht DHT;
 
 
int counter;
 
byte mac[] = { 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xdd };

IPAddress zabbix(192, 168, 7, 7);

int zabbixPort = 10051;

char host[] = "ethernet";     // hostname zabbix
char key[][11] = {"other","humidity","temp"};   

int numeroDeVariaveis = 3; //COLOQUE AQUI A QUANTIDADE DE VARIÁVEIS QUE SERÃO ENVIADAS PARA O ZABBIX
 
void zabbix_send_trap(void);
 
void setup() {
    Serial.begin(9600);
    Serial.println("Obtaining IP address using DHCP...");
    while (Ethernet.begin(mac) != 1) {
        delay(10);
    }
    counter = 1;
    Serial.println("setup done.");
    printIPAddress();
}
 
void loop() {
    zabbix_send_trap();
    delay(2000*15);
    counter++;
    DHT.read11(dht_apin);
    Serial.print("Current humidity = ");
    Serial.print(DHT.humidity);
    Serial.print("%  ");
    Serial.print("temperature = ");
    Serial.print(DHT.temperature); 
    Serial.println("C  ");

}
 
const char PROGMEM content_header[] = "{\"request\":\"sender data\",\"data\":[";
const char PROGMEM content_item[] = "{\"host\":\"%s\",\"key\":\"%s\",\"value\":\"%d\"}";
const char PROGMEM content_footer[] = "]}";
 
       
 
unsigned int prepare_content_hum(char *dst, char keyAtual[]) {
    unsigned int len = 0;
 
    memcpy(&dst[len], content_header, sizeof(content_header));
    len += sizeof(content_header) - 1;
    len += sprintf_P(&dst[len], content_item, host, keyAtual, DHT.humidity);
    memcpy(&dst[len], content_footer, sizeof(content_footer));
    len += sizeof(content_footer) - 1;
    return len;
}


unsigned int prepare_content_temp(char *dst, char keyAtual[]) {
    unsigned int len = 0;
 
    memcpy(&dst[len], content_header, sizeof(content_header));
    len += sizeof(content_header) - 1;
    len += sprintf_P(&dst[len], content_item, host, keyAtual, DHT.temperature);
    memcpy(&dst[len], content_footer, sizeof(content_footer));
    len += sizeof(content_footer) - 1;
    return len;
}
 


 
EthernetClient zbx_client;
char packet_header[] = "ZBXD\1"; //followed by unsigned long long content_len
unsigned long long content_len;
unsigned int payload_len;
char packet_content[256];
 
void zabbix_send_trap() {

      content_len = prepare_content_temp(packet_content, key[2]);
      payload_len = sizeof(content_header) + content_len + sizeof(content_footer);
      if (zbx_client.connect(zabbix, zabbixPort)) {
          Serial.println("connected to zabbix");
          Serial.println("sending data");
          zbx_client.write(packet_header, sizeof(packet_header) - 1);
          zbx_client.write((char *)&content_len, sizeof(content_len));
          zbx_client.write(packet_content, content_len);
          delay(1);
          zbx_client.stop();
          Serial.println("disconnected");
      }

delay(10);
      content_len = prepare_content_hum(packet_content, key[1]);
      payload_len = sizeof(content_header) + content_len + sizeof(content_footer);
      if (zbx_client.connect(zabbix, zabbixPort)) {
          Serial.println("connected to zabbix");
          Serial.println("sending data");
          zbx_client.write(packet_header, sizeof(packet_header) - 1);
          zbx_client.write((char *)&content_len, sizeof(content_len));
          zbx_client.write(packet_content, content_len);
          delay(1);
          zbx_client.stop();
          Serial.println("disconnected");
      }

}


void printIPAddress()
{
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }

  Serial.println();
}
